package com.yh.homestay.controller;

import com.yh.homestay.common.JsonUtils;
import com.yh.homestay.pojo.UserStaff;
import com.yh.homestay.service.UserStaffServiceG;
import com.yh.homestay.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;


/**
 * 页面跳转Controller
 *
 * @author Administrator
 */
@Controller
@Api(description = "页面跳转")
public class PageController {

    private Log logger = LogFactory.getLog(PageController.class);
    @Autowired
    private UserStaffServiceG userStaffServiceG;
    @Autowired
    private RedisUtil redisUtil;


    @Value("${REDIS.CONTENT}")
    private String redisContent;

    /**
     * @param model
     * @Date: 2020/1/13 16:37
     * @Description: 登录
     * @return: java.lang.String
     */
    @RequestMapping("/")
    @ApiOperation(value = "登录", notes = "登录时将信息放到redis中或者从redis中获取登录信息")
    //从前台页面获取staffId之后，查询结果并返回对象，前台页面通过data.staffId获取
    public String toIndex(Model model,HttpServletRequest request) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Integer staffId = Integer.parseInt(authentication.getName());
        try {
            String json = (String) redisUtil.hget("redisContent", staffId + "");
            if (StringUtils.isNotBlank(json)) {
                model.addAttribute("staffId", json);
            }
        } catch (Exception e) {
            logger.info(e.getMessage());
        }
        model.addAttribute("staffId", staffId);
        request.getSession().setAttribute("staff",staffId);
        UserStaff userStaff = userStaffServiceG.selectOne(staffId);
        String s = JsonUtils.objectToJson(userStaff);
        try {
            redisUtil.hset("redisContent", staffId + "", s, 600*3);
        } catch (Exception e) {
            logger.info(e.getMessage());
        }
        return "index.html";
    }

    /**
     * @param
     * @Date: 2020/1/13 16:37
     * @Description: 转跳登录页面
     * @return: java.lang.String
     */
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    @ApiOperation(value = "转跳登录页面", notes = "转跳登录页面")
    public String login() {
        return "login.html";
    }

    /**
     * @Date: 2020/1/13 16:38
     * @Description: 页面跳转
     * @param page
     * @return: java.lang.String
     */
//    @RequestMapping(value = "/{page}",method = RequestMethod.GET)
//    public String toPage(@PathVariable("page") String page) {
//        return page;
//    }

}
